import { useLocation, useNavigate, useRoutes } from 'react-router-dom'
import routes from '@/routes'
import { useGetLocalStorage } from '@/hooks/useLocalStorage'
import { TOKEN } from '@/common/constant'
import { useEffect } from 'react'

function App() {
  const element = useRoutes(routes)
  const local = useGetLocalStorage(TOKEN)
  const navigateTo = useNavigate()
  const { pathname } = useLocation()

  useEffect(() => {
    if (!local) {
      navigateTo('/login')
    } else {
      pathname === '/login' ? navigateTo('/') : null
    }
  }, [])

  return <>{element}</>
}

export default App
